<?php
namespace Wx\Controller;
use Think\Controller;

class SoldOutController extends CommonController
{
    // 商品售罄页面
    public function index(){
        $shop_spellgroup = D('shop_spellgroup');//拼团表
        $shop_spellgroup_copy = D('shop_spellgroup_copy');//小区副本
        $category = D('category');
        $member = D('member');
        $shoparr = $shop_spellgroup->where("state=1 and type=0")->order("id desc")->select();

        //查询当前用户小区id
        $c_id = $member->where("id=".session('u_id'))->getField('c_id');//当前用户信息
        //查询本小区门店信息
        $s_id = M("shop_store")->where("c_id = {$c_id}")->getField('id');//获取该社区绑定店铺
        if(!empty($s_id)){
            //本社区店拼团
            $shop_md = $shop_spellgroup->where("state=1 and type=1 and s_id=".$s_id)->order("id desc")->select(); //门店产品
            $shoparr = array_merge($shoparr,$shop_md);
        }
        $shoparr = array_slice($shoparr,0,4);
        foreach($shoparr as $i=>$one){
            if($c_id>0){//已经绑定小区
                //用户本小区拼团信息
                $getcopy = $shop_spellgroup_copy->where("sg_id=".$one['id']." and c_id=".$c_id)->find();//用户本小区该商品是否开团

                if($getcopy){
                    $shoparr[$i]['havecopy'] = 1;//表示已经开团
                    if($one['min_num']- $getcopy['num']>0){
                        $shoparr[$i]['neednum'] = $one['min_num']- $getcopy['num'];//用户本小区还差几人份成团
                    }else{
                        $shoparr[$i]['neednum'] = 0;//拼团成功，还可以继续参团
                    }//end if
                }else{
                    $shoparr[$i]['havecopy'] = 0;//未开团

                }//end if
            }else{
                //未绑定小区
                $shoparr[$i]['havecopy'] = 0;//未开团
            }//end if
            //单位 
            $getu=$category->where("id=".$one['unit'])->find();//dump($getu);
            $shoparr[$i]['unit'] = $getu['title'];
            //产品第一图片
            $img=explode(',',$one['h_imgs']);
            $shoparr[$i]['f_img'] = $img[0];//第一张图片作为封面图
            //发布人头像、姓名
            $getinfo = $member->where("id=".$one['u_id'])->field('realname,avatar')->find();
            $shoparr[$i]['f_realname'] = $getinfo['realname'];
            $shoparr[$i]['f_avatar'] = $getinfo['avatar'];
            $shoparr[$i]['actual_num'] = $shop_spellgroup_copy->where("sg_id=".$one['id'])->sum('num');//已售多少份
        }//end foreach
        $this->assign('shoparr',$shoparr);
        $this->display('soldout');
    }//end function Index()





}//end class